//
//  ScrollContentVIew.m
//  哈哈
//
//  Created by 奇艺果 on 2017/12/25.
//  Copyright © 2017年 奇艺果. All rights reserved.
//

#import "ScrollContentVIew.h"

@interface ScrollContentVIew()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UILabel *titleLineLabel;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, weak)   UIButton *selectorButton;
@end

@implementation ScrollContentVIew

- (instancetype)initWithTitleArray:(NSArray *)strArray{
    self = [super init];
    if(self){
        self.titleArray = strArray;
        [self setupUI];
    }
    return self;
}
#pragma mark target

- (void)titleBtnClick:(UIButton *)btn
{
    //1. 切换按钮
    [self titleChangeButton:btn];
    //2. 添加对应的控制器view到滚动区
    self.collectionView.contentOffset = CGPointMake(btn.tag * SCREEN_WIDTH, 0);
    
}
#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.titleArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = indexPath.row == 0 ? [UIColor yellowColor] : indexPath.row == 1 ? [UIColor grayColor] : [UIColor blueColor];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击了----%ld",indexPath.row);
    
}

#pragma mark -UIScrollViewDelegate
/**
 *  滚动完成的时候
 *
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / scrollView.width;
    //2. 选中按钮
    [self titleChangeButton:self.buttons[i]];
}

#pragma mark -内部方法
/**
 *  切换按钮
 *
 */
- (void)titleChangeButton:(UIButton *)btn
{
    [self.selectorButton setTitleColor:[UIColor colorWithWhite:1 alpha:0.5] forState:UIControlStateNormal];
    self.selectorButton.titleLabel.font = ScaleFont(15);
    self.selectorButton.transform = CGAffineTransformIdentity;
    
    [btn setTitleColor:[UIColor colorWithWhite:1 alpha:1] forState:UIControlStateNormal];
    btn.titleLabel.font = ScaleBoldFont(18);
    self.selectorButton = btn;
    NSString *content = btn.titleLabel.text;
    UIFont *font = btn.titleLabel.font;
    CGSize size = CGSizeMake(MAXFLOAT, 30.0f);
    CGSize buttonSize = [content boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine  | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName:font} context:nil].size;
    self.titleLineLabel.width = buttonSize.width;
    self.titleLineLabel.centerX = btn.centerX;
    
}
#pragma mark setUI
- (void)setupUI{
    
    [self addSubview:self.titleView];
    [self addSubview:self.collectionView];
    self.collectionView.contentSize = CGSizeMake(self.titleArray.count *SCREEN_WIDTH, 0);
}

- (UICollectionView *)collectionView
{
    if(!_collectionView)
    {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, ScaleH(30), self.flowLayout.itemSize.width, self.flowLayout.itemSize.height) collectionViewLayout:self.flowLayout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.pagingEnabled = YES;
//        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:DDMainCollectionCell];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)flowLayout
{
    if(!_flowLayout)
    {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _flowLayout;
}

- (UIView *)titleView
{
    if(!_titleView)
    {
        _titleView = [[UIView alloc] init];
        _titleView.backgroundColor = [UIColor redColor];
        _titleView.size = CGSizeMake(SCREEN_WIDTH, ScaleH(30));
        [_titleView addSubview:self.titleLineLabel];
        self.titleLineLabel.y = self.titleView.height - self.titleLineLabel.height;
        CGFloat x = 0;
        CGFloat y = 0;
        CGFloat w = self.titleView.width/self.titleArray.count;
        CGFloat h = self.titleView.height;
        for (int i = 0; i <self.titleArray.count; i++) {
            x = i *w;
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
            btn.tag = i;
            [btn setTitle:self.titleArray[i] forState:UIControlStateNormal];
            btn.titleLabel.font = ScaleFont(15);
            [btn setTitleColor:[UIColor colorWithWhite:1 alpha:0.5] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            if (i == 0)
            {
                [self titleBtnClick:btn];
            }
            [self.titleView addSubview:btn];
            [self.buttons addObject:btn];
        }
    }
    return _titleView;
}

- (UILabel *)titleLineLabel
{
    if(!_titleLineLabel)
    {
        _titleLineLabel = [[UILabel alloc] init];
        _titleLineLabel.backgroundColor = [UIColor whiteColor];
        _titleLineLabel.size =CGSizeMake(20, 2);
    }
    return _titleLineLabel;
}

- (NSMutableArray *)buttons
{
    if (_buttons == nil)
    {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}
@end
